﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace myClient
{
    using MSNPSharp;
    using MSNPSharp.Core;

    public partial class frmSelectRemote : Form
    {
        private frmMain _clientform;

        public Contact SelectedContact = null;

        public frmSelectRemote(frmMain client)
        {
            InitializeComponent();
            this._clientform = client;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (tvFavoriteList.SelectedNode != null && tvFavoriteList.SelectedNode.Level > 0)
            {
                SelectedContact = (Contact)tvFavoriteList.SelectedNode.Tag;
                this.DialogResult = DialogResult.OK;
            }
        }

        public void SortByStatus()
        {
            this.tvFavoriteList.BeginUpdate();
            this.tvFavoriteList.Nodes.Clear();

            TreeNode onlinenode = tvFavoriteList.Nodes.Add("0", "Online", 0, 0);
            //onlinenode.NodeFont = PARENT_NODE_FONT;
            onlinenode.Tag = "0";
            TreeNode offlinenode = tvFavoriteList.Nodes.Add("1", "Offline", 0, 0);
            //offlinenode.NodeFont = PARENT_NODE_FONT;
            offlinenode.Tag = "1";

            foreach (Contact contact in _clientform.pMessenger.ContactList.All)
            {
                if (contact.IsMessengerUser)
                {
                    TreeNode newnode = contact.Online ? onlinenode.Nodes.Add(contact.Mail, contact.Name) : offlinenode.Nodes.Add(contact.Mail, contact.Name);
                    //newnode.NodeFont = contact.Blocked ? USER_NODE_FONT_BANNED : (contact.DynamicChanged != DynamicItemState.None) ? PARENT_NODE_FONT : USER_NODE_FONT;
                    newnode.Tag = contact;
                }
            }

            onlinenode.Text = "Online (" + onlinenode.Nodes.Count.ToString() + ")";
            offlinenode.Text = "Offline (" + offlinenode.Nodes.Count.ToString() + ")";

            tvFavoriteList.Sort();
            tvFavoriteList.EndUpdate();
            tvFavoriteList.AllowDrop = false;
        }

        private void tvFavoriteList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tvFavoriteList.SelectedNode != null && tvFavoriteList.SelectedNode.Level > 0)
                btnOk.Enabled = true;
            else
                btnOk.Enabled = false;
        }
    }
}
